package com.syaccp.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.syaccp.model.Staff;
import com.syaccp.model.SysDept;
import com.syaccp.model.TOrderDetail;
import com.syaccp.request.OrderDetailRequest;

import com.syaccp.response.Company;
import com.syaccp.response.StaffTaskDate;

import com.syaccp.service.StaffService;
import com.syaccp.service.TOrderDetailService;
import com.syaccp.utils.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chen
 * @since 2023-07-10
 */
@RestController
//@RequestMapping("/api/staff")
@RequestMapping("/staff")
@CrossOrigin
public class StaffController {
    @Resource
    private StaffService staffService;
    @Resource
    private TOrderDetailService iOrderDetailService;

    @ApiOperation(value = "确定指派订单")
    @ApiImplicitParam()
    @PutMapping("/confirmAssignTask")

//    public Result  confirmAssignTask(@PathVariable List<OrderDetail> list){
        public Result  confirmAssignTask(@RequestBody OrderDetailRequest orderDetailRequest){
        List<TOrderDetail> list = orderDetailRequest.getList();
        for (TOrderDetail orderDetail : list) {
            Staff staff = staffService.QueryStaffById(orderDetail.getMakeId());

            orderDetail.setMakeName(staff.getMakeName());
            orderDetail.setMakeId(staff.getMakeId());
            Integer integer = staffService.AssignmentOrder(orderDetail);

            //修改成功时，如果该制作员的状态为空闲，则改变状态为忙碌
            if (staff.getStatus() == 1) {
                Integer integer1 = staffService.UpdateStaffStatus(staff);
                System.out.println("integer1= " + integer1);
            }

            //修改任务订单状态
            Integer integer2 = staffService.UpdateStatusInMake(orderDetail.getId());
            System.out.println("integer2 = " + integer2);

        }

        return Result.success(1);
    }



    @ApiOperation(value = "根据制作组id和公司id查询制作员信息")
    @ApiImplicitParam()
    @GetMapping("/QueryStaffMsg")
    public Result  QueryStaffMsg(Staff staff){
        List<Staff> staff1 = staffService.QueryStaffMsg(staff);
        return Result.success(staff1);
    }



    @ApiOperation(value = "查询任务状态为等待中的任务信息")
    @ApiImplicitParam()
    @GetMapping("/QueryWaitingTask")
    public Result QueryWaitingTask(){
        PageInfo<TOrderDetail> orderDetailPageInfo = staffService.QueryWaitingTask();
        List<TOrderDetail> list = orderDetailPageInfo.getList();
        return Result.success(orderDetailPageInfo);
    }



    @ApiOperation(value = "动态查询分组信息")
    @ApiImplicitParam()
    @GetMapping("/QueryMakeGroup")
    public Result QueryMakeGroup(Integer id){
        List<SysDept> sysDepts = staffService.QueryMakeGroup(id);
        return Result.success(sysDepts);
    }



    @PutMapping(value = "EndCurrentStaffState/{id}")
    @ApiOperation(value = "结束状态",notes = "结束忙碌状态")
    @ApiImplicitParam(name = "id", value = "制作员id")
    public Result EndCurrentStaffState(@PathVariable Integer id){
        System.out.println("ssssss====");
        System.out.println("id = " + id);
        //先修改任务表中属于该制作员并且状态为制作中和完成小图的任务
        Integer integer = iOrderDetailService.UpdateTaskStatesByStaffId(id);
        System.out.println("integer = " + integer);
        Integer integer1=0;
        if (integer>0){
            System.out.println("zzzz");
            //修改员工表中制作员状态为空闲
            integer1= staffService.EndCurrentStaffState(id);
        }
        return Result.success(integer1);
    }


    @ApiOperation(value = "动态查询公司信息")
    @ApiImplicitParam()
    @GetMapping("/QueryCompanyMsg")
    public Result QueryCompany(){
        Result result=new Result();

        List<Company> companyList=new ArrayList<>();

        List<SysDept> sysDepts = staffService.QueryCompany();
        if(sysDepts!=null) {
            for (SysDept sysDept : sysDepts) {
                Company company=new Company();
                company.setCompanyId(sysDept.getId());
                company.setCompanyName(sysDept.getName());
                companyList.add(company);
            }
            for (Company company : companyList) {
                System.out.println("company = " + company);
            }

            result.setCode(200);
            result.setData(companyList);
            return result;
        }else {
            result.setCode(500);
        }


        return result;

    }

    @ApiOperation(value = "分页查询所有制作员信息")
    @ApiImplicitParam()
    @GetMapping("/QueryAllStaff")
    public Result QueryAllStaffMsg(Staff staff) throws ParseException {
        Result result=new Result();

        PageInfo<Staff> staffPageInfo = staffService.QueryAllStaffByPage(staff, staff.getIndex());
        List<Staff> list = staffPageInfo.getList();

        if (list!=null){
            //查询成功
            result.setCode(200);
            for (Staff staff1 : list) {
                //制作员状态为忙碌时，查询该制作员任务数
               if (staff1.getStatus()==2){
                   staff1.setStatusMsg("忙碌");
                   staff1.setColor("danger");
                   //制作员手上的任务数量
                   Integer integer = staffService.QueryStaffMakeNum(staff1);
                       staff1.setOrderDetailNum(integer);

                   //查询制作员的当前任务时长
                   StaffTaskDate staffTaskDate = staffService.QueryMakeNameTaskDate(staff1);
                   staff1.setStaffTaskDate(staffTaskDate);
                   System.out.println("staffTaskDate = " + staffTaskDate);
               }

               else {
                   staff1.setStatusMsg("空闲");
                   staff1.setColor("success");
                   //空闲制作员赋予暂无任务的默认值
                   staff1.setOrderDetailNum(0);
               }
            }

            staffPageInfo.setList(list);
            result.setData(staffPageInfo);
            return  result;

        }else {
            //查询失败
            result.setCode(500);
        }
        return result;
    }


}
